﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using Inicio.Entities;

namespace Inicio.Forms.Obras
{
    public partial class AddObra : Form
    {
        public List<TextBox> textValidators = new List<TextBox>();
        public List<ComboBox> cbValidators = new List<ComboBox>();
        public List<Control> spoutValidators = new List<Control>();
        public List<Spout> spouts = new List<Spout>();

        public AddObra()
        {
            InitializeComponent();
        }

        private void AddObra_Load(object sender, EventArgs e)
        {
            CargarTextos();
            DataGridView1_Load();

            textValidators.Add(textBox1);
            textValidators.Add(textBox2);
            cbValidators.Add(comboBox1);
            cbValidators.Add(comboBox2);
            cbValidators.Add(comboBox3);
            cbValidators.Add(comboBox4);
            ValidateButton1();
            cb1Items();
            cb5Items();
            LoadCountrys();

            spoutValidators.Add(textBox4);
            spoutValidators.Add(textBox5);
            spoutValidators.Add(textBox6);
            spoutValidators.Add(textBox7);
            ValidateButton3();
             
        }

        #region CargaTextos

        private void CargarTextos()
        {
            TextosButtons();
            TextosLabels();
            TextosTextBoxs();
        }

        private void TextosButtons()
        {
            button1.Text = "Crear";
            button2.Text = "Cerrar";
            button3.Text = "Agregar Caño";
        }

        private void TextosLabels()
        {
            label1.Text = "Sub-Empresa";
            label2.Text = "Prestación";
            label3.Text = "Número de Contrato";
            label4.Text = "Nombre de la Obra";
            label6.Text = "Comitente";
            label7.Text = "Empresa Constructora";
            label8.Text = "Subcontratista";
            label9.Text = "Inspección";
            label10.Text = "Inicio de Obra";
            label11.Text = "Fin de Obra";
            label12.Text = "Monto";
        }

        private void TextosTextBoxs()
        {
            textBox4.Text = "Diametro";
            textBox5.Text = "Presión";
            textBox6.Text = "Rigidez";
            textBox7.Text = "Longitud";
        }


        #endregion

        #region DataGrid
        private void DataGridView1_Load()
        {
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.Columns.Add(CreateColumn("Diametro", "Column1", 150));
            dataGridView1.Columns.Add(CreateColumn("Presión", "Column2", 150));
            dataGridView1.Columns.Add(CreateColumn("Rigidez", "Column3", 150));
            dataGridView1.Columns.Add(CreateColumn("Longitud", "Column4", 150));
            dataGridView1.Columns.Add(new DataGridViewButtonColumn());
            dataGridView1.ColumnHeadersDefaultCellStyle.Font = new System.Drawing.Font(dataGridView1.ColumnHeadersDefaultCellStyle.Font.FontFamily, 12);
            dataGridView1.DefaultCellStyle.Font = new Font(dataGridView1.DefaultCellStyle.Font.FontFamily, 12);
        }

        private DataGridViewColumn CreateColumn(string header, string prop, int width)
        {
            DataGridViewColumn res = new DataGridViewTextBoxColumn();
            res.HeaderText = header;
            res.DataPropertyName = prop;
            res.Width = width;

            return res;
        }

        #endregion

        #region RestablecerCamposSpout

        private void textBox1_MouseClick(object sender, MouseEventArgs e)
        {
            ((TextBox)sender).ResetText();
        }

        private void textBox3_Leave(object sender, EventArgs e)
        {
            if (textBox3.Text == "")
            {
                textBox3.Text = "Ciudad";
            }
        }

        private void textBox4_Leave(object sender, EventArgs e)
        {
            if (textBox4.Text == "")
            {
                textBox4.Text = "Diametro";
            }
        }

        private void textBox5_Leave(object sender, EventArgs e)
        {
            if (textBox5.Text == "")
            {
                textBox5.Text = "Presión";
            }
        }

        private void textBox6_Leave(object sender, EventArgs e)
        {
            if (textBox6.Text == "")
            {
                textBox6.Text = "Rigidez";
            }
        }

        private void textBox7_Leave(object sender, EventArgs e)
        {
            if (textBox7.Text == "")
            {
                textBox7.Text = "Longitud";
            }
        }
        #endregion

        #region TextBox
        
        private void textBox12_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsLetter(e.KeyChar) || char.IsWhiteSpace(e.KeyChar) || char.IsSymbol(e.KeyChar))
            {
                MessageBox.Show("Solo se permiten números.");
                e.Handled = true;
                return;
            }
            ValidateButton1();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            ValidateButton1();
        }

        private bool DefaultText(string textbox)
        {
            int number;
            bool res = Int32.TryParse(textbox, out number);
            return res;
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            ValidateButton3();
        }
        #endregion

        #region ComboBoxes

        private void cb5Items()
        {
            Core.Connection conn = new Core.Connection();

            List<Currency> dataBind = new List<Currency>();
            dataBind.AddRange(conn.LoadCurrency());

            comboBox5.ValueMember = "id";
            comboBox5.DisplayMember = "description";
            comboBox5.DataSource = dataBind;

        }
        private void cb1Items()
        {
            Core.Connection conn = new Core.Connection();

            List<Enterprise> dataBind = new List<Enterprise>();

            Enterprise defaultEnterprise = new Enterprise(0, "Prestador");
            dataBind.Add(defaultEnterprise);
            dataBind.AddRange(conn.LoadLenders());

            comboBox1.ValueMember = "id";
            comboBox1.DisplayMember = "name";
            comboBox1.DataSource = dataBind;
            
        }

        private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            cb2Items();
        }

        private void cb2Items()
        {
            List<Rendering> dataBind = new List<Rendering>();

            Rendering defaultRendering = new Rendering(0, "Tipo de Obra");
            dataBind.Add(defaultRendering);

            try
            {
                Core.Connection conn = new Core.Connection();

                dataBind.AddRange(conn.LoadWorkTypes(Convert.ToInt32(comboBox1.SelectedValue)));
            }
            catch
            {
            }
            comboBox2.ValueMember = "id";
            comboBox2.DisplayMember = "name";
            comboBox2.DataSource = dataBind;
        }

        public void LoadCountrys()
        {
            Core.Connection conn = new Core.Connection();

            List<Country> dataBind = new List<Country>();

            Country defaultCountry = new Country(0, "País");
            dataBind.Add(defaultCountry);
            dataBind.AddRange(conn.LoadCountrys());

            comboBox3.ValueMember = "Id";
            comboBox3.DisplayMember = "Name";
            comboBox3.DataSource = dataBind;
        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<Province> dataBind = new List<Province>();

            Province defaultProvince = new Province(0, "Provincia");
            dataBind.Add(defaultProvince);

            try
            {
                Core.Connection conn = new Core.Connection();

                dataBind.AddRange(conn.LoadProvinces(Convert.ToInt32(comboBox3.SelectedValue)));
            }
            catch { }
            comboBox4.ValueMember = "id";
            comboBox4.DisplayMember = "name";
            comboBox4.DataSource = dataBind;

            ValidateButton1();

        }

        private void comboBox4_SelectedValueChanged(object sender, EventArgs e)
        {
            ValidateButton1();
        }
        
        #endregion

        #region Botones

        private void button1_Click(object sender, EventArgs e)
        {
            Obra obra = new Obra();
            obra.obraName = textBox2.Text;
            obra.obraType = (int)comboBox1.SelectedValue;
            obra.style = (int)comboBox2.SelectedValue;
            obra.contractNumber = textBox1.Text;
            obra.countryId = Convert.ToInt32(comboBox3.SelectedValue);
            obra.provinceId = Convert.ToInt32(comboBox4.SelectedValue);
            obra.city = textBox3.Text;
            obra.comitente = textBox8.Text;
            obra.constructionCompany = textBox9.Text;
            obra.subContractor = textBox10.Text;
            obra.inspection = textBox11.Text;
            obra.initiation = dateTimePicker1.Value; //.ToShortDateString();
            obra.finish = dateTimePicker2.Value; //.ToShortDateString();
            obra.currency = Convert.ToInt32(comboBox5.SelectedValue);
            obra.amount = Convert.ToSingle(textBox12.Text);

            Core.Connection cn = new Core.Connection();
            cn.NewObra(obra, spouts);
            this.Dispose();
            this.Close();
    }

        

        private void button3_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Add(textBox4.Text, textBox5.Text, textBox6.Text, textBox7.Text, "Borrar");
            
            Spout spout = new Spout();
            spout.diameter = Convert.ToInt32(textBox4.Text);
            spout.pressure = Convert.ToInt32(textBox5.Text);
            spout.rigidity = Convert.ToInt32(textBox6.Text);
            spout.length = Convert.ToInt32(textBox7.Text);
            spouts.Add(spout);

            TextosTextBoxs();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        private void ValidateButton3()
        {
            button3.Enabled = spoutValidators.All(x => DefaultText(x.Text) == true);
        }


        private void ValidateButton1()
        {
            button1.Enabled = !(cbValidators.Any(x => Convert.ToInt32(x.SelectedValue) == 0))
                                && !(textValidators.Any(x => x.Text == ""));
        }
        #endregion

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.Columns[e.ColumnIndex].CellType.Name == new DataGridViewButtonColumn().CellType.Name)
            {
                if (dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString() == "Borrar")
                {
                    dataGridView1.Rows.RemoveAt(e.RowIndex);
                    dataGridView1.Refresh();
                }
            }
        }
    }
}